package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.BookIslandGameBean;
import com.rjwh.dingdong.client.util.ViewHolder;
import com.rjwh.dingdong.client.widget.RoundImageView;

/**
 * @author Kay
 * @ClassName MathGameBookIslandGameAdapter
 * @Description 岛的游戏目录列表
 * @date 2016-8-31
 * @history 2016-8-31 author: description:
 */
public class MathGameBookIslandGameAdapter extends AdapterBase<BookIslandGameBean> {
    private OnItemButtonClickListener buttonClickListener;
    private Context context;

    public MathGameBookIslandGameAdapter(Context context,OnItemButtonClickListener buttonClickListener) {
        this.context = context;
        this.buttonClickListener = buttonClickListener;
    }

    @Override
    protected View getExView(int position, View convertView, ViewGroup parent) throws Exception {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.math_game_island_game_item, null);
        }
        RoundImageView img = ViewHolder.get(convertView, R.id.math_game_book_island_game_item_img);
        TextView titleTv = ViewHolder.get(convertView, R.id.math_game_book_island_game_item_title);
        TextView descTv = ViewHolder.get(convertView, R.id.math_game_book_island_game_item_desc);
        Button startBtn = ViewHolder.get(convertView, R.id.math_game_book_island_game_item_btn);

        BookIslandGameBean bookIslandGameBean = getList().get(position);
        if (bookIslandGameBean == null) {
            return convertView;
        }
        String gameimage = bookIslandGameBean.getGameimage();
        String gamename = bookIslandGameBean.getGamename();
        String gamedesc = bookIslandGameBean.getGamedesc();
        final String bookid = bookIslandGameBean.getBookid();
        final String lessonid = bookIslandGameBean.getLessonid();
        final String gamedir = bookIslandGameBean.getGamedir();
        final String gameid = bookIslandGameBean.getGameid();
        Glide.with(context).load(gameimage).dontAnimate().placeholder(R.drawable.icon_pic_holder).error(R.drawable.icon_pic_holder)
                .diskCacheStrategy(DiskCacheStrategy.ALL).into(img);
        if (gamename != null) {
            titleTv.setText(gamename);
        }
        if (gamedesc != null) {
            descTv.setText(gamedesc);
        }

        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonClickListener.onItemButtonClick(bookid,lessonid,gamedir,gameid);
            }
        });

        return convertView;
    }

    public interface OnItemButtonClickListener {
        void onItemButtonClick(String bookid, String lessonid, String gamedir, String gameid);
    }
}
